<%@page import="cn.edu.pku.wxlib.usertobookborrow.model.*"%>

<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib tagdir="/WEB-INF/tags/simpletable" prefix="simpletable"%>
<%@ include file="/commons/taglibs.jsp"%>

<rapid:override name="head">
	<title>图书流通</title>
	<!-- 1在借，2已还，3欠款，4丢失 -->
</rapid:override>

<rapid:override name="content">
	<div class="search">
		<div class="form-group inline icon">
			<i class="icon-reorder"></i> <input type="text"
				class="form-field col-1-5" id="bookName" placeholder="输入图书名称">
		</div>

		<div class="form-group inline icon">
			<i class="icon-flag"></i> <input type="text" class="form-field col-1"
				id="author" placeholder="输入作者">
		</div>

		<!-- <div class="form-group inline">
			<select id="isRenew" class="form-field col-1">
				<option value="">续借状态</option>
				<option value="1">已续借</option>
				<option value="0">可续借</option>
			</select>
		</div> -->

		<div class="form-group inline icon">
			<i class="icon-calendar"></i> <input type="text"
				class="form-field col-1" id="borrowTimeBegin" placeholder="借书开始">
		</div>
		<strong>至</strong>
		<div class="form-group inline icon">
			<i class="icon-calendar"></i> <input type="text"
				class="form-field col-1" id="borrowTimeEnd" placeholder="借书结束">
		</div>

		<div class="form-group inline icon">
			<i class="icon-calendar"></i> <input type="text"
				class="form-field col-1" id="shouldReturnTimeBegin"
				placeholder="应还开始">
		</div>
		<strong>至</strong>
		<div class="form-group inline icon">
			<i class="icon-calendar"></i> <input type="text"
				class="form-field col-1" id="shouldReturnTimeEnd" placeholder="应还结束">
		</div>

		<div class="form-group inline icon search-btn"
			onclick="queryBookList(0);" title="查询">
			<i class="icon-search"></i>
		</div>

	</div>
	<!-- end of search -->

	<%-- <div class="handleControl">
		<div class="form-group inline">
			<button type="button" class="btn btn-primary"
				onclick="window.location = '${ctx}/usertobookborrow/new'">新增</button>
			<button type="button" class="btn btn-default"
				onclick="doRestBatchDelete('${ctx}/usertobookborrow','items',document.forms.queryForm)">删除</button>
		</div>
	</div> --%>

	<div class="page" id="fullpage">
		<!-- start of sidebar -->
		<div id="sidebar" class="pull-left">
			<div class="operation line" id="categoryBlock">
				<div class="header">
					<div class="header-left header-nav">
						<a href="javascript:void(0);"> <i
							class="header-icon icon-home"></i>
						</a>
					</div>
					<h1 class="header-title">
						<a href="javascript:void(0);">图书分类</a>
					</h1>
				</div>
				<div class="operation-body"
					style="display: none; padding-left: 20px;">
					<ul class="menu_tree"> 
					<li>
						<span style="padding-left:30px"> 
						<a href="javascript:;" data-categoryId="15">TP自动化技术计算技术</a>
						</span>
					</li>
					</ul>
				</div>
			</div>
		</div>
		<!--  end of sidebar -->
		
		<!-- start of book 0 全部，1可借，2可续借，3已续借，4已还，5欠款，6已丢失 -->
		<div class="rightpage pull-right">
			<ul id="status-nav" class="nav-right">
				<li id="all" class="active"><a href="javascript:;"
					onclick="queryByStatus(this,0)" class="all"> 全部图书 </a></li>
				<li id="canborrow"><a href="javascript:;"
					onclick="queryByStatus(this,1);"> 可借图书&nbsp;&nbsp; <span
						class="number"></span>
				</a></li>
				<li id="canreborrow"><a href="javascript:;"
					onclick="queryByStatus(this,2);"> 可续借图书&nbsp;&nbsp; <span
						class="number"></span>
				</a></li>
				<li id="reborrowed"><a href="javascript:;"
					onclick="queryByStatus(this,3);"> 已续借图书&nbsp;&nbsp; <span
						class="number"></span>
				</a></li>
				<!-- confirmed -->
				<li id="returned"><a href="javascript:;"
					onclick="queryByStatus(this,4);"> 已还图书&nbsp;&nbsp; <span
						class="number"></span>
				</a></li>
				<!-- <li id="fined"><a href="javascript:;"
					onclick="queryByStatus(this,5);"> 欠款图书&nbsp;&nbsp; <span
						class="number"></span>
				</a></li> -->
				<!-- picked -->
				<li id="lost"><a href="javascript:;"
					onclick="queryByStatus(this,6);"> 已丢失图书&nbsp;&nbsp; <span
						class="number"></span>
				</a></li>
				<!-- packed -->
			</ul>

			<div class="orderlist">
				<table id="bookTable" class="table text-center">
					<!-- orderTable -->
					<thead>
						<tr>
							<th class="col3">图书名称</th>
							<th class="col3">详细信息</th>
							<th class="col0">书架</th>
							<th class="col3">操作</th>
						</tr>
					</thead>
					<tbody>

					</tbody>
				</table>
				<div id="loading" class="loading">
					<i class="icon-spinner icon-spin"></i>
				</div>
				<div id="pagination"></div>
			</div>
			<!-- end of table  -->
		</div>
		<!-- end of right  -->
		<div class="clear"></div>
	</div>

	<!-- 图书外借 -->
	<div class="modal fade" id="borrow_dialog">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title">图书外借</h4>
				</div>
				<div class="modal-body">
					<div class="form-group inline" style="line-height: 55px">
						<label>图书名称</label> <input class="form-field col-3" type="text"
							name="bookName">
						<!-- <label class="form-field col-3" id="bookNamelabel"></label> -->
					</div>

					<div class="form-group inline" style="line-height: 55px">
						<label>学号/教工号</label> <input class="form-field col-3" type="text"
							name="userName" placeholder="输入学号/教工号">
					</div>

					<!-- <div class="form-group inline" style="line-height:55px">
						<label>姓名</label> <input class="form-field col-3" type="text"
							name="name" placeholder="输入姓名">
					</div> -->
				</div>
				<div class="modal-footer">
					<input type="hidden" name="bookSkuid" />
					<button type="button" id="submitBtn" class="btn btn-primary"
						onclick="submit_form(this);">确认外借</button>
					<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>

	<!-- 还书跟踪 -->
	<div class="modal fade" id="ordertracing">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<!-- <button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button> -->
					<h4 class="modal-title" style="width: 560px;">图书流通记录</h4>
				</div>
				<div class="modal-body">
					<ul id="timeline">
					</ul>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<!-- /.modal -->
	<!-- 添加图书外借记录 -->
</rapid:override>
<rapid:override name="script">
	<script src="${ctx}/scripts/rest.js"></script>
	<link href="<c:url value="/widgets/simpletable/simpletable.css"/>"
		type="text/css" rel="stylesheet">
	<script type="text/javascript"
		src="<c:url value="/widgets/simpletable/simpletable.js"/>"></script>

	<script type="text/javascript">
		// 基础数据
		var root = "/PkuWxLibrary";
		var bookborrow_url = root + "/usertobookborrow";
		var options = {
			bookTable : 'bookTable', // 图书显示的table的id
			categoryBlock : 'categoryBlock', // 分类div的id
			countBlock : 'countBlock', // 技术div的id
			base_url :''
		};
		// 请求状态变化的url字符串
		var urlStrings = new Array("","s_borrow","s_reborrow","s_return","s_lost");
		var buttonString = new Array("","图书外借","图书续借","图书归还","图书丢失");
		var arr = new Array();
		//初始化时间选择器
		var picker1 = new Pikaday(
	    {
	        field: document.getElementById('borrowTimeBegin'),
	        firstDay: 1,
	        minDate: new Date('1910-01-01'),
	        maxDate: new Date('2100-12-31'),
	        yearRange: [2000,2020]
	    });
	    var picker2 = new Pikaday(
	    {
	        field: document.getElementById('borrowTimeEnd'),
	        firstDay: 1,
	        minDate: new Date('1910-01-01'),
	        maxDate: new Date('2100-12-31'),
	        yearRange: [2000,2020]
	    });
		var picker3 = new Pikaday(
		{
			field: document.getElementById('shouldReturnTimeBegin'),
			firstDay: 1,
			minDate: new Date('1910-01-01'),
			maxDate: new Date('2100-12-31'),
			yearRange: [2000,2020]
		});    
		var picker4 = new Pikaday(
		{
			 field: document.getElementById('shouldReturnTimeEnd'),
			 firstDay: 1,
			 minDate: new Date('1910-01-01'),
			 maxDate: new Date('2100-12-31'),
			 yearRange: [2000,2020]
		});
		
		var statuses = new Array(<c:forEach items="${state}" var="item" >"${item.manageName}",</c:forEach>"");
		var statusIndex = new Array(<c:forEach items="${skustates}" var="item" >"${item.key}",</c:forEach>"");
		var sortColumns = "";
		var pageNumber = 1;
		var rootCategoryId = 1;				
		var parentCategory = rootCategoryId; 
		
		var targets = new Array("all","canborrow","canreborrow","reborrowed","returned","fined","lost");
		function moveAnimation(source,index){
			var tmp ;
	        if(tmp) tmp.remove(); 
	       
	        tmp = $(source).clone();
	        var p= source.offset();
	        var target = $("#"+targets[index]);
	        var offset = target.offset();
	        var p2 = {left:offset.left,top:offset.top-50};
	        tmp.addClass('_box').css(p).insertBefore(source);
	        p2=$.extend(p2,{height:20,width:20,padding:0,opacity:10});
	        $(tmp).animate(p2, "slow",function(){
	        	tmp.remove();
	        	if(index<6){
	        		var span = target.find("span.number");
	            	span.text(parseInt(span.text())+1);
	            	span = target.prev().find("span.number");
	            	span.text(parseInt(span.text())-1);
	        	}else{
	        		var span = target.find("span.number");
	        		    span.text(parseInt(span.text())-1);
	        		
	        	}
	        });
		}
	
		/**
		 * 请求分类数据
		 * @param url  请求路径
		 * @param requsetData 请求数据
		 */
		function loadCategoryData(url,requsetData){
			post(url,requsetData,function(data){
				var target = $("#"+options.categoryBlock);
				var category = data.category;
				var a = target.find(".header-title").children("a");
				a.text(category.categoryName+"区域");
				a.attr("onclick","queryByCategory(event,this,"+category.categoryId+");");
				target.children(".operation-body").append(createMenuTree(category.childrens,1));
				
				target.children(".operation-body").slideToggle();
			});
		}
		
		function createCategoryblock(data,target){
			$.each(data,function(index,item){
			
				var dl = $("<dl class='sidebar-item'></dl>");
				var dt = $("<dt class='sidebar-title'><a href='javascript:;' onclick='queryByCategory(event,this,"+item.categoryId+");'>"+item.categoryName+"</a></dt>");
				var dd = $("<dd class='sidebar-content collapse'></dd>");
				if(item.childrens.length>0)
					dd.append(addData2List(item.childrens,"sidebar-nav"));
				else
					dt.addClass("no-arrow");
				dl.append(dt);
				dl.append(dd);
				target.append(dl);
			});

		}
		
		/**
		 * 创建分类list
		 * @param data 分类数据
		 * @param style css样式
		 */
		function addData2List(data,style){
			var ul = $("<ul class='"+style+"'></ul>");
			$.each(data,function(index,item){
				var li = $("<li><a href='javascript:;' onclick='queryByCategory(event,this,"+item.categoryId+");'>"+item.categoryName+"</a></li>");
				
				if(item.childrens.length>0){
				
					li.append(addData2List(item.childrens,"second-level"));
				}
				ul.append(li);
			});
			return ul;
		}
		
		/**
		 * 生成树状菜单
		 * @param data
		 * @returns
		 */
		function createMenuTree(data,index){
			var ul = $("<ul class='menu_tree'></ul>");
			var padding = 15*(index+1);
			$.each(data,function(i,item){
				var li = $("<li></li>");
				var a = $("<a href='javascript:;' onclick='queryByCategory(event,this,"+item.categoryId+");'>"+item.categoryName+"</a>");
				var span = $("<span style='padding-left:"+padding+"px'></span>");
				var children = "";
				if(item.childrens.length>0){
					a.addClass("parent");
					children = createMenuTree(item.childrens,index+1);
				}
				span.append(a);
				li.append(span);
				if(children!=""){
					children.hide();
					li.append(children);
				}
					
				ul.append(li);
			});
			return ul;
		}
		
		
		$(window).load(function() {
			var index = 0;
			/* $("#categoryBlock").menuTree({requestUrl:"/PkuWxLibrary/category/query", requestData:JSON.stringify({parentCategoryId:parentCategory})},function(event,which,id){
				queryByCategory(event, which, id);
			}); */
			queryBookList(index);
		});
		
		// 点击分类，查询图书
		function queryByCategory(event, which, id) {
			//阻止事件广播
			event.stopPropagation();    //标准   
	        event.cancelBubble = true;  //IE
	        
			parentCategory = id;
			queryBookList();
		}
		
		function refresh(){
			var index = 0;
			queryBookList(index);
		}
		
		function queryByStatus(which, index) {
			queryBookList(index);
			var li = $(which).parent();
			if(!li.hasClass("active")){
				li.siblings(".active").removeClass("active");
				li.addClass("active");
			}
		}
		
		function queryBookList(index) {
			var conditions = getQueryConditions(index);
			countBookByStatus(bookborrow_url + "/count", conditions);
			
			$(".search").find("input[type='text']").val("");
			$("#pagination").empty();
			loadBookData(bookborrow_url + "/list", index, conditions, "queryBook");
			/* if (index == 3) { // 已还图书
				loadBookData(bookborrow_url + "/returnlist",index, getQueryConditions(index), "queryBook");
			} else {
				loadBookData(bookborrow_url + "/list", index, getQueryConditions(index), "queryBook");
			} */
		}
		
		// 统计各状态图书数量
		function countBookByStatus(url, queryData) {
			post(url, queryData, function(data){
				$.each(data.data, function(index, item){
					var target = getStatusTarget(index+1);
					if (target != ""){
						target.find(".number").text(item.userToBookBorrowId);
					}
				});
			});
		}
		// target 0 全部，1可借，2可续借，3已续借，4已还，5欠款，6已丢失
		function getStatusTarget(status) {
			var target = "";
			switch(status) {
			case 1:
				target = $("#canborrow");
				break;
			case 2:
				target = $("#canreborrow");
				break;
			case 3:
				target = $("#reborrowed");
				break;
			case 4:
				target = $("#returned");
				break;
			case 5:
				target = $("#fined");
				break;
			case 6:
				target = $("#lost");
				break;
			default:
				target = "";
				break;
			}
			return target;
		} 
		
		// 请求流通图书数据
		function loadBookData(url, indexflag, requestData, callback) {
			var tbody = $("#bookTable").find("tbody");
			var empty = true;
			if(empty)
				tbody.empty();// 清除tbody
			$("#loading").show();
				
			post(url, requestData, function(data){
				var style = "";
				
				if(data.resultCode == "100") {
					$.each(data.data, function(index,item){
						var trId = item.bookSkuid;
						var tr = $("<tr id=tr_"+trId+" class=''></tr>");
						arr[index] = item.bookSkuid;
						var bookName = (item.bookName != null) ? item.bookName : "";
						var td_bookName = $("<td class=''>"+bookName+"</td>");
						var td_detail = $("<td class=''></td>");
						var author = (item.author != null) ? item.author : "";
						var publiser = (item.publiser != null) ? ("<br>" + item.publiser) : "";
						var edition = (item.edition != null) ? ("<br>第" + item.edition + "版") : "";
						var td_info = $("<div class='info'>"+author + publiser + edition + "</div>");
						td_detail.append(td_info);
						var shelfName =  (item.shelfName != null) ? item.shelfName : "";
						var td_shelf = $("<td class=''>"+shelfName+"</td>");
						var operation = $("<td></td>");
						
						if (indexflag == 4) {
							/* var button = $("<input type='button' id='btn_return' class='btn "+style+"' value='查看流通记录' data-id='"+item.bookSkuid+ " data-borrowid='" + item.userToBookBorrowId + "'
									onclick = 'findReturnTracing(this,"+item.bookSkuid+","+item.userToBookBorrowId+");'/>"); */
							var href = $("<a href='javascript:;' class='trace operation' data-id='"+item.bookSkuid+ "' data-borrowid='"+item.userToBookBorrowId +"'>图书流通记录</a>");
							operation.append(href);
						} else { 
							setBookStatus(parseInt(item.skustate), item.bookSkuid,item.bookName, item.userId, operation);
						}
						
						tr.append(td_bookName);
						tr.append(td_detail);
						tr.append(td_shelf);
						tr.append(operation);
						
						tbody.append(tr);
					});
					
					$("#loading").hide();
					getPageNation($("#pagination"),data.total,data.pageNum,data.currentPage,callback);
					
					$.each($(".pop-btn"),function(index,item){
					    initPop(index,item);
					});
					
				}
			});
		}
		
		// 已还图书的操作：查看所有操作记录
		$(document).on("click",".trace",function(){
		var loading = $("<a href='javascript:;' class='operation'><i class='icon-spinner icon-spin'></i>&nbsp;数据加载中…</a>");
		var skuid = $(this).attr("data-id");
		var borrowid = $(this).attr("data-borrowid");
		var trace_url = bookborrow_url+ "/" + borrowid + "/returntracing";
		var a = $(this);
		a.hide();
		loading.insertBefore(a);
		
		post(trace_url,{},function(data){
			a.show();
			loading.remove();
			var target = $("#timeline");
			target.empty();
			if(data.resultCode == "100"){
				$(data.list).each(function(i,item){
					var li = $("<li></li>");
					var span = $("<span class='round'></span>");
					var p =$("<p></p>");
					var text = item.operateTimeString;
					var name = "操作人:" + item.name;
					if(item.name == null)
						name = "";
						
					text += " "+name;
					
					var userName = ", 读者:" + item.userName;
					if(item.userName == null)
						userName = "";
						
					text += userName;
					
					if(item.operateType == 1){
						text += ", 图书外借";
					} else if (item.operateType == 2) {
						text += ", 续借";
					} else if (item.operateType == 3) {
						text += ", 还书";
					} else if (item.operateType == 4) {
						text += ", 图书丢失";
					} else {
						text += " ";
					}
					
					if(item.remark != null)
						text += " "+item.remark;
					
					p.text(text);
					li.append(span);
					li.append(p);
					target.append(li);
					
				});
			}else if (data.resultCode == "101") {
				var li = $("<li></li>");
				var alert = $("<div class='alert alert-warning text-center' style='margin:0px;'>"+
							"未找到流通记录</div>");
				li.append(alert);
				target.append(li);
			}
			else{
				var li = $("<li></li>");
				var alert = $("<div class='alert alert-warning text-center' style='margin:0px;'>"+
							"查询图书流通记录</div>");
				li.append(alert);
				target.append(li);
			}
			$("#ordertracing").modal({backdrop: 'static'});
		});
	});
		
		// 根据不同的图书SKU状态，生成不同的操作按钮
		//sku 1非外借，2在架，3在借，4续借，5已预约，6下架，7报废，8丢失
		//bookborrow1在借，2续借，3已还，4欠款，5丢失
		function setBookStatus(state, bookSkuid, bookName,userId, operation){
			var text = "";
			var style = "";
			var index = -1;
			var bookSkuid = bookSkuid;
			var userId = userId;
			
			switch(state) {
			case -1:
				text = "";
				break;
			case 1:
				text = "不可外借";
				style = "btn-default";
				index = 0;
				break;
			case 2:
				text = "可外借";
				style = "btn-primary";
				index = 1;
				break;
			case 3:
				text = "已外借";
				style = "btn-success";
				index = 2;
				break;
			case 4:
				text = "已续借";
				style = "btn-warning";
				index = 3;
				break;
			case 5:
				text = "已预约";
				style = "btn-primary";
				index = 1;
				break;
			case 6:
				text = "已下架";
				style = "btn-default";
				index = 0;
				break;
			case 7:
				text = "已报废";
				style = "btn-default";
				index = 0;
				break;
			case 8:
				text = "已丢失";
				style = "btn-danger";
				index = 0;
				break;
			}
			
			operation.append($("<span class='order-status status-"+state+ "'>" + text +"</span>")); 
			/* operation.append($("<span class='order-status status-4'>" + text +"</span>"));		 */
			
			if (index > 0) {
				if(index == 1) { // 可外借
					var button = $("<br><input type='button' id='btn_status_borrow' class='btn "+style+"' value='"+
							buttonString[index]+"' data-id='" + bookSkuid + "' data-name='" + bookName +"' onclick = 'bookBorrow(this,"+bookSkuid+");'/>");
					operation.append(button);
				} else {
					if (index == 2) { // 已外借，可续借
						var button = $("<br><input type='button' id='btn_status_reborrow' class='btn "+style+"' value='"+
								buttonString[index]+"' data-id='"+bookSkuid+ "' data-userid='" + userId + "' onclick = 'changeBookBorrowState(this,"+bookSkuid+","+state+",2);'/>");
						operation.append(button);
					}
					//
					var button = $("<br><input type='button' id='btn_status_return' class='btn btn-success' value='"+
							buttonString[3]+"' data-id='" + bookSkuid + "' data-userid='" + userId + "' onclick = 'changeBookBorrowState(this,"+bookSkuid+","+state+",3);'/>");
					operation.append(button);
					var button = $("<br><input type='button' id='btn_status_lost' class='btn btn-danger' value='"+
							buttonString[4]+"' data-id='"+bookSkuid+ "' data-userid='" + userId + "' onclick = 'changeBookBorrowState(this,"+bookSkuid+","+state+",4);'/>");
					operation.append(button);
				}
			}
		}
		
		/** 图书外借 */
		function bookBorrow(which, bookSkuid) {
			var bookSkuid = $(which).attr("data-id");
			var bookName = $(which).attr("data-name");
			$(which).hide();
			
			var borrow_dialog = $("#borrow_dialog");
			borrow_dialog.find("input[type='text']").val("");
			/* $("#bookNamelabel").val(bookName); */
			borrow_dialog.find("input[name='bookName']").val(bookName);
			borrow_dialog.find("input[name='bookSkuid']").val(bookSkuid);
			
			$("#borrow_dialog").modal('toggle');
		}
		/** 确认外借 */
		function submit_form(which) {
			//var urlStrings = new Array("","s_borrow","s_reborrow","s_return","s_lost");
			//var buttonString = new Array("","图书外借","图书续借","图书归还","图书丢失");
			var borrow_dialog = $("#borrow_dialog");
			var bookSkuid = borrow_dialog.find("input[name='bookSkuid']").val();
			var userName = borrow_dialog.find("input[name='userName']").val();
			
			var url = bookborrow_url + "/" + bookSkuid + "/" +  userName + "/" + urlStrings[1]; // 
			$(which).attr("disabled", true);
			$(which).text("保存中…");
			post(url, {}, function(data){
				if(data.resultCode == "100") {
					var tr = $("#tr_"+bookSkuid);
					var td = tr.children("td").eq(3);
					td.empty();
					setBookStatus(parseInt(data.skustate), bookSkuid, data.bookName, data.userId, td);
					tr.append(td);
					var target = $("#canborrow").find(".number");
					target.text(parseInt(target.text())-1);
					var targetReborrow = $("#canreborrow").find(".number");
					targetReborrow.text(parseInt(targetReborrow.text())+1);
				} else if (data.resultCode == "101") {
					var alert = createAlert("未找到读者信息！", "warning");
					alert.insertBefore($("#fullpage"));
					setHideEvent(alert, function() {
					});
				} else if (data.resultCode == "102") {
					var alert = createAlert("未找到图书信息！", "warning");
					alert.insertBefore($("#fullpage"));
					setHideEvent(alert, function() {
					});
				} else {
					var alert = createAlert("服务器内部错误，请联系管理员。", "warning");
					alert.insertBefore($("#fullpage"));
					setHideEvent(alert, function() {
					});
				}
			});
			$(which).removeAttr("disabled");
			$(which).text("确认外借");
			$('#borrow_dialog').modal('toggle');
		}
		
		/** 图书续借，归还 ，丢失*/
		function changeBookBorrowState(which, bookSkuid,state,index) {
			var bookSkuid = $(which).attr("data-id");
			var userId = $(which).attr("data-userid");
			$(which).hide();
			var i = $("<i class='icon-spinner icon-spin'></i>");
			//var urlStrings = new Array("","s_borrow","s_reborrow","s_return","s_lost");
			//var buttonString = new Array("","图书外借","图书续借","图书归还","图书丢失");
			//sku 1非外借，2在架，3在借，4续借，5已预约，6下架，7报废，8丢失
			//target 0 全部，1可借，2可续借，3已续借，4已还，5欠款，6已丢失
			//index - 操作类型 2续借，3还，4丢失
			var url = bookborrow_url + "/" + bookSkuid + "/" +  userId + "/" + urlStrings[index];
			post(url, {}, function(data){
				if(data.resultCode == "100") {
					var tr = $("#tr_"+bookSkuid);
					var td = tr.children("td").eq(3);
					td.empty();
					setBookStatus(parseInt(data.skustate), bookSkuid, data.bookName, userId, td);
					tr.append(td);
					if(index == 2){ // 续借
						var targetCanReBorrow = getStatusTarget(index).find(".number");
						targetCanReBorrow.text(parseInt(targetCanReBorrow.text())-1);
						var targetReBorrowed = getStatusTarget(index+1).find(".number");
						targetReBorrowed.text(parseInt(targetReBorrowed.text())+1);
					} else if (index == 3){ // 还
						if(state == 3) { // “在借”时还
							var targetCanBorrow = getStatusTarget(2).find(".number");
							targetCanBorrow.text(parseInt(targetCanBorrow.text())-1);
						} else if (state == 4) { // “续借”时还
							targetCanReBorrow = getStatusTarget(3).find(".number");
							targetCanReBorrow.text(parseInt(targetCanReBorrow.text())-1);
						} else {
						}
						var targetReturn = getStatusTarget(index+1).find(".number");
						targetReturn.text(parseInt(targetReturn.text())+1);
						var targetCanBorrowbook = getStatusTarget(1).find(".number"); //可借+1
						targetCanBorrowbook.text(parseInt(targetCanBorrowbook.text())+1);
					} else if (index == 4) { // 丢失
						if(state == 3) { // “在借”时丢失
							var targetCanBorrow = getStatusTarget(2).find(".number");
							targetCanBorrow.text(parseInt(targetCanBorrow.text())-1);
						} else if (state == 4) { // “续借”时丢失
							targetCanReBorrow = getStatusTarget(3).find(".number");
							targetCanReBorrow.text(parseInt(targetCanReBorrow.text())-1);
						} else {
						}
						var targetLost = getStatusTarget(index+2).find(".number");
						targetLost.text(parseInt(targetLost.text())+1);
					} else {
					}
				}  else {
					var alert = createAlert("服务器内部错误，请联系管理员。", "warning");
					alert.insertBefore($("#fullpage"));
					setHideEvent(alert, function() {
					});
				}
			});
		} 
		
		// 获取订单查询条件
		function getQueryConditions(index) {
			var dataString = "'pageNumber':'" + pageNumber+"'";
			var bookName = $("#bookName").val();
			var author = $("#author").val();
			//var isRenew = $("#isRenew").val();
			var borrowTimeBegin = $("#borrowTimeBegin").val();
			var borrowTimeEnd = $("#borrowTimeEnd").val();
			var shouldReturnTimeBegin = $("#shouldReturnTimeBegin").val();
			var shouldReturnTimeEnd = $("shouldReturnTimeEnd").val();
			var state = index;
			
			if(parentCategory>1)
				dataString += ",'categoryId':'"+parentCategory+"'";
			if(bookName != "" && bookName != undefined)
				dataString += ",'bookName':'"+ bookName + "'";
			if(author != "" && author != undefined)
				dataString += ",'author':'"+ author + "'";
			if(borrowTimeBegin != ""&& borrowTimeBegin != undefined)
				dataString += ",'borrowTimeBegin':'"+ borrowTimeBegin + "'";
			if(borrowTimeEnd != "" && borrowTimeEnd != undefined)
				dataString += ",'borrowTimeEnd':'"+ borrowTimeEnd + "'";
			if(shouldReturnTimeBegin != "" && shouldReturnTimeBegin != undefined)
				dataString += ",'shouldReturnTimeBegin':'"+ shouldReturnTimeBegin + "'";
			if(shouldReturnTimeEnd != "" && shouldReturnTimeEnd != undefined)
				dataString += ",'shouldReturnTimeEnd':'"+ shouldReturnTimeEnd + "'";
			if(state != "" && state != undefined && state != 0)
				dataString += ",'state':'"+ state + "'";
			
			return JSON.stringify(eval("({"+dataString+"})"));
		}
		
		// 提示信息显示
		function createAlert(msg, style) {
			var p = $("<p>" + msg + "</p>");
			var alert = $("<div class='alert alert-" + style + "'></div>");
			alert.append(p);
			return alert;
		}
		// 提示信息隐藏
		function setHideEvent(alert, callback) {
			setTimeout(function() {
				alert.fadeOut("slow", function() {
					alert.remove();
					if (callback != undefined && typeof (callback) == "function")
						callback();
				});
			}, 1500);
		}
		
		$(document).ready(function() {
			window.simpleTable = new SimpleTable('queryForm',${page.thisPageNumber},${page.pageSize},'${pageRequest.sortColumns}');
		});
	</script>
</rapid:override>
<%@ include file="base.jsp"%>
